package com.gykfbxxh.imovie.view.user.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.gykfbxxh.imovie.AppApplication;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.adapter.FilmCommentAdapter;
import com.gykfbxxh.imovie.base.BaseListActivity;
import com.gykfbxxh.imovie.bean.comment.FilmComment;
import com.gykfbxxh.imovie.bean.comment.FilmCommentBean;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.listener.HttpListener;
import com.gykfbxxh.imovie.utils.HttpUtils;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的评论
 */
public class MyCommentActivity extends BaseListActivity<FilmComment> {
    private List<FilmComment> mCommentList;//新闻评论集合
    //适配器
    private FilmCommentAdapter mCommentAdapter;//评论适配器
    private int mMark;//下拉刷新请求标识
    @Override
    protected void changeView() {
        mPageName = "MyCommentActivity" ;
        setTitleShow(true);
        setTitle("我的评论");
    }

    @Override
    protected void changeData(final boolean isRefresh) {

        HttpUtils.addGetRequest(URIContant.GETMYCOMMENTLIST1_0 + dataPage, new TypeToken<FilmCommentBean>() {
        }.getType(), "FilmDetailActivity", new HttpListener<FilmCommentBean>() {
            @Override
            public void onStart() {
                //设置正在刷新
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                //设置刷新状态
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(FilmCommentBean response) {
                if (response!=null&&response.getResult()!=null) {
                    FilmCommentBean.FilmCommentResult result = response.getResult();
                    List<FilmComment> list = result.getList();

                    if (mCommentList == null) {
                        mCommentList = new ArrayList<FilmComment>();
                    }
                    if (mCommentAdapter == null) {
                        mCommentAdapter = new FilmCommentAdapter(mCommentList, mActivity);
                        mListView.setAdapter(mCommentAdapter);
                    }
                    if(isRefresh) {
                        mCommentList.clear();
                    }
                    if (list != null && list.size() > 0) {
                        mCommentList.addAll(list);
                        mCommentAdapter.notifyDataSetChanged();
                        setLoadView(LOADSUCES, "");
                    } else if (mCommentList != null && mCommentList.size() > 0) {
                        //展示没有更多评论
                        setLoadView(LOADSUCES, "没有更多评论,点击重试");
                    } else {
                        mCommentAdapter.notifyDataSetChanged();
                        setLoadView(LOADSUCES, "暂无评论");
                    }
                } else {
                    //当前没有评论则显示加载错误
                    setLoadView(LOADSUCES, getString(R.string.pullup_nomore_data));
                }
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        if(mCommentList==null) {
            return;
        }
        //防止越界
        if (position >= mCommentList.size()) {
            position = mCommentList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        FilmComment filmComment = mCommentList.get(position);
        if(filmComment!=null&&filmComment.getFilm()!=null&& !TextUtils.isEmpty(filmComment.getFilm().getFId())) {
            Intent intent = new Intent(this, FilmDetailActivity.class);
            intent.putExtra(PARAMETER1,filmComment.getFilm().getFId());
            startActivity(intent);
        }

    }
}
